﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Evolution.Structure.Globe.Details.Cities;
using System.ComponentModel;
using System.Windows.Input;
using Evolution.Structure.Globe.Fields;
using EvolutionBaseTypes.Interfaces;
using Evolution.Structure.Globe.Details;
using System.Windows;

namespace GameProject.ViewModel
{
    class MainGameViewModel : ViewModelBase
    {
        private Map _myMap;

        public MainGameViewModel()
        {
            Map myMap = new Map();
            MapViewModel mapViewModel = new MapViewModel(this);
            myMap.DataContext = mapViewModel;
            _MainContent = myMap;
            _myMap = myMap;
        }


        #region Properties


        public String BuildCityName { get; set; }

        #region IsCitySelected

        public bool IsCitySelected
        {
            get
            {
                if (SelectedCity != null) return true;
                return false;
            }
        }

        #endregion //IsCitySelected

        #region BuildModeActivated

        public bool BuildModeActivated
        {
            get { return _BuildModeActivated; }
            set
            {
                this._BuildModeActivated = value;
                base.onPropertyChanged("BuildModeActivated");
            }
        }
        private bool _BuildModeActivated;

        #endregion //BuildModeActivated

        #region MainContent

        public UserControl MainContent
        {
            get { return _MainContent; }
            set { _MainContent = value;
                  base.onPropertyChanged("MainContent");     
            }
        }
        private UserControl _MainContent;

        #endregion MainContent

        #region CityList

        public BindingList<City> CityList
        {
            get { return GlobalVar.CurrentPlayer.Cities; }
        }

        #endregion //CityList

        #region SelectedCity

        public City SelectedCity { 
            get { return _SelectedCity; }
            set { _SelectedCity = value;
                  if(value != null) 
                      this.openCityView();
                  base.onPropertyChanged("SelectedCity");
                  base.onPropertyChanged("IsCitySelected");
            }
        }
        private City _SelectedCity;

        #endregion //SelectedCity


        #endregion //Properties


        #region Commands


        #region BackToMapCommand

        public ICommand BackToMapCommand
        {
            get
            {
                if (_BackToMapCommand == null)
                {
                    _BackToMapCommand = new RelayCommand(param => showMap(), null);
                }
                return _BackToMapCommand;
            }
        }
        private ICommand _BackToMapCommand;

        #endregion //BackToMapCommand

        #region EndTurnCommand

        public ICommand EndTurnCommand
        {
            get
            {
                if (_EndTurnCommand == null)
                {
                    _EndTurnCommand = new RelayCommand(param => endTurn(), null);
                }
                return _EndTurnCommand;
            }
        }
        private ICommand _EndTurnCommand;

        #endregion //EndTurnCommand


        #endregion //Commands


        #region Methods


        public void openCityView()
        {
            MainContent = new CityView();
            MainContent.DataContext = new CityViewViewModel(SelectedCity);
        }

        public void showMap()
        {
            MainContent = _myMap;
            SelectedCity = null;
        }


        #region FieldActivated

        public void FieldActivated(Field field)
        {
            if (BuildModeActivated == true &&
                (field.Type != FieldType.Water)
                && field.Type != FieldType.Mountain
                && field.HasCity == false)
            {
                CityName cityName = new CityName();
                cityName.DataContext = this;
                
                BuildCityName = null;
                bool? result = cityName.ShowDialog();

                if (BuildCityName == null) MessageBox.Show("Stadtname darf nicht leer sein ;-)");

                foreach (City city in GlobalVar.CurrentPlayer.Cities)
                {
                    if (city.Name == BuildCityName)
                    {
                        MessageBox.Show("Stadtname ist bereits vorhanden");
                        return;
                    }
                }

                if (result.HasValue)
                    if (result.Value == true)
                    {
                        //get fieldlist (influencedArea)
                        field.City = new City(field.Position, field, BuildCityName);
                        field.City.ByCultureInfluencedArea = GlobalVar.World.getWorldFieldList(field.Position, Constants.START_INFLUENCE_AREA, true);
                        GlobalVar.CurrentPlayer.Cities.Add(field.City);
                    }

                BuildModeActivated = false;
            }
            else
            {
                //City CLick
                if (field.HasCity == true)
                    SelectedCity = field.City;

                // Baumodus = AUS
                if(BuildModeActivated == true)
                    BuildModeActivated = false;
            }
        }

        #endregion //FieldActivated

        #region endTurn

        private void endTurn()        
        {
            try
            {
                City selectedCity = null;
                //close cityview if open and reopen after
                if (IsCitySelected)
                {
                    selectedCity = SelectedCity;
                    showMap();
                }

                //Berechnung der hinzukommenden Ressourcen
                foreach (City city in GlobalVar.CurrentPlayer.Cities)
                {
                    foreach (Field field in city.ByCultureInfluencedArea)
                    {
                        GlobalVar.CurrentPlayer.PlayerResources += field.HarvestableResources;
                    }
                }

                //Building Queues abarbeiten
                foreach (City city in GlobalVar.CurrentPlayer.Cities)
                {
                    while (city.ConstructingQueue.Count > 0)
                    {
                        IBuildable buildable = city.ConstructingQueue.First();

                        if (GlobalVar.CurrentPlayer.PlayerResources.MoneyPoints.Value >= buildable.BuildingCosts)
                        {
                            GlobalVar.CurrentPlayer.PlayerResources.MoneyPoints.Value -= buildable.BuildingCosts;
                            city.ConstructingExpenses += buildable.BuildingCosts;
                            if (buildable is Building)
                                city.Buildings.Add((Building)buildable);
                            city.ConstructingQueue.Dequeue();

                            //what to do with units/Citizens???
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                //reopen CityView if it was open before
                if (selectedCity != null)
                {
                    SelectedCity = selectedCity;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        #endregion //endTurn


        #endregion //Methods
    }
}
